package com.lagou.service.impl;

import com.lagou.dao.TokenDao;
import com.lagou.pojo.LagouToken;
import com.lagou.service.CodeService;
import com.lagou.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    //@Autowired
    //private CodeClient client;
    @Reference
    private CodeService codeService;

    @Autowired
    private TokenDao tokenDao;

    @Override
    public Integer register(String email, String password, String code, HttpServletResponse response) {
        //校验验证码
        Integer validate = codeService.validate(email, code);
        switch (validate) {
            case 0:
                System.out.println("验证码正确");
                break;
            case 1:
                System.out.println("验证码错误");
                return 1;
            case 2:
                System.out.println("验证码超时");
                return 2;
        }
        //注册成功
        //1。生成token
        String token = UUID.randomUUID().toString();
        //2。添加cookie
        Cookie cookie = new Cookie("token", token);
        cookie.setPath("/");
        response.addCookie(cookie);
        //3。存入数据库
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);
        lagouToken.setEmail(email);
        lagouToken.setPassword(password);
        tokenDao.save(lagouToken);
        return 0;
    }

    @Override
    public Boolean isRegistered(String email) {
        LagouToken lagouTokenExample = new LagouToken();
        lagouTokenExample.setEmail(email);
        Example<LagouToken> example = Example.of(lagouTokenExample);
        LagouToken lagouToken = tokenDao.findOne(example).get();
        if (Objects.isNull(lagouToken.getId())) {
            return false;
        }
        return true;
    }

    @Override
    public String login(String email, String password, HttpServletResponse response) {
        LagouToken lagouTokenExample = new LagouToken();
        lagouTokenExample.setEmail(email);
        lagouTokenExample.setPassword(password);
        Example<LagouToken> example = Example.of(lagouTokenExample);
        LagouToken lagouToken = tokenDao.findOne(example).get();
        if (Objects.isNull(lagouToken.getId())) {
            return "false";
        }
        //添加cookie
        String token = UUID.randomUUID().toString();
        Cookie cookie = new Cookie("token", token);
        cookie.setPath("/");
        response.addCookie(cookie);
        //存入数据库
        lagouToken.setToken(token);
        tokenDao.save(lagouToken);
        //返回email
        return lagouToken.getEmail();
    }

    @Override
    public String info(String token) {
        LagouToken lagouTokenExample = new LagouToken();
        lagouTokenExample.setToken(token);
        Example<LagouToken> example = Example.of(lagouTokenExample);
        LagouToken lagouToken = tokenDao.findOne(example).get();
        return lagouToken.getEmail();
    }
}
